package com.lyf.service;

import com.alibaba.fastjson.JSON;
import com.lyf.annotation.LogTrace;
import com.lyf.base.BaseService;
import com.lyf.dao.UserMapper;
import com.lyf.domain.User;
import com.lyf.exception.ParamsException;
import com.lyf.util.HttpClientUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.time.LocalTime;

@Service
@CacheConfig(cacheNames = {"userCache"})
@Log4j2
public class UserService extends BaseService<User> {

    @Autowired
    private UserMapper userMapper;

    @Cacheable
    @LogTrace
    public User queryUser (Integer id){
        return userMapper.queryById(id);
    }

    @Cacheable(value = "defaultCache",key = "#id")
    public User queryUser2 (Integer id){
        return userMapper.queryById(id);
    }

    @Retryable(value = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 2000, multiplier = 1.5))
    public void retry() throws Exception {
        if(true){
            log.info("重试: {}", LocalTime.now());
            throw new Exception();
        }
    }

    @Recover
    public void recover(Exception e){
        log.error("重试次数已超出! {}", LocalTime.now());
        throw new ParamsException("重试次数已超出");
    }

}
